package com.example.mapinformationandroid.UI.Fragment;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.mapinformationandroid.ConstantData.Define;
import com.example.mapinformationandroid.Http.HTTPBase;
import com.example.mapinformationandroid.Model.ResponseBase;
import com.example.mapinformationandroid.R;
import com.example.mapinformationandroid.UI.Login;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class SignUpFragment extends Fragment implements View.OnClickListener {
    private EditText name, phone, password;
    private Button btn_sign_in;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment__sign_up, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        name = view.findViewById(R.id.sign_up_et_name);
        phone = view.findViewById(R.id.sign_up_et_phone);
        password = view.findViewById(R.id.sign_up_et_passwords);
        btn_sign_in = view.findViewById(R.id.btn_sign_in);
        btn_sign_in.setOnClickListener(this);
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign_in:
                signIn(getData());

        }
    }

    private Map<String, String> getData() {
        Map<String, String> signData = new ArrayMap<>();
        if (name.getText() != null && phone.getText() != null && password.getText() != null) {
            signData.put(Define.UserDefine.USER_NAME, name.getText().toString());
            signData.put(Define.UserDefine.USER_PHONE, phone.getText().toString());
            signData.put(Define.UserDefine.USER_HEAD_PICTURE,Define.UserDefine.DEFAULT_PICTURE);
            signData.put(Define.UserDefine.UER_PASSWORD, password.getText().toString());
            return signData;
        } else {
            Toast.makeText(getContext(), "请输入完整信息", Toast.LENGTH_SHORT).show();
            return signData;
        }
    }

    private void signIn(Map<String, String> map) {
        HTTPBase httpBase = new HTTPBase();
        httpBase.sign_in(map, new Observer<ResponseBase>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResponseBase value) {
                if (value.getCode() == Define.HttpDefine.HTTP_SUCCESS) {
                    Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    translateFragment();
                }else {
                    Toast.makeText(getContext(), "注册失败,请检查账号信息是否正确", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void translateFragment() {
        Login login = (Login)getActivity();
        login.switchLogin();
    }
}
